package com.softfuxi.tbs.app.utils;

import android.content.Context;

import com.amap.api.location.AMapLocation;

/**
 * AUTHOR : 葛立平
 * TODO : APP启动获取的位置信息
 * DATE : 2019/9/10
 * VERSION : 1.0
 */
public class StartLocationTools {

    private AMapLocation aLocation;                 //当前位子信息

    public interface IResultListener {
        void onLocation(AMapLocation aMapLocation);
    }

    private static class StartLocationnHolder {
        private static final StartLocationTools INSTANCE = new StartLocationTools();
    }

    public static StartLocationTools getInstance() {
        return StartLocationnHolder.INSTANCE;
    }

    public void initLocation(Context context, final IResultListener resultListener) {

        if (aLocation != null && resultListener != null) {
            resultListener.onLocation(aLocation);
        } else {
            LocationTools locationTools = new LocationTools(context);
            locationTools.start(new LocationTools.OnLocationResultListener() {
                @Override
                public void onLocationSuccess(AMapLocation aMapLocation) {
                    aLocation = aMapLocation;
                    if (resultListener != null) {
                        resultListener.onLocation(aLocation);
                    }
                }

                @Override
                public void onLocationFailed() {
                    if (resultListener != null) {
                        resultListener.onLocation(null);
                    }
                }
            }, 500);
        }
    }

    public void clean(){
        aLocation = null;
    }

    public void initLocation(Context context) {
        initLocation(context, null);
    }
}
